package com.watson.juc.testlist;

import java.util.Collection;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author watson
 * @date 2021/7/30 11:44
 * @Description: TODO
 **/
public class BlockingQueueDemo {
    public static void main(String[] args) {
        //什么情况下我们使用阻塞队列，
//        test01();
//        test02();
//        test03();
        test04();
    }

    /**
     * 抛出异常
     */
    public static void test01() {
        // 队列的大小
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
        System.out.println(blockingQueue.add("a"));
        System.out.println(blockingQueue.add("b"));
        System.out.println(blockingQueue.add("c"));
        // IllegalStateException: Queue full
//       System.out.println(blockingQueue.add("d"));

        System.out.println(blockingQueue.element());
        System.out.println("===================");
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        //NoSuchElementException
        System.out.println(blockingQueue.remove());

    }

    /**
     * 有返回值，没有异常
     */
    public static void test02() {
        // 队列的大小
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("c"));
        System.out.println(blockingQueue.offer("d"));
        System.out.println(blockingQueue.peek());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());

    }

    /**
     * 等待 阻塞（一直zus）
     * 等待 阻塞（超时）
     */
    public static void test03() {
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
        try {
            blockingQueue.put("a");
            blockingQueue.put("a");
            blockingQueue.put("a");
            //没有位置了一直阻塞
//            blockingQueue.put("a");

            System.out.println("============");
            System.out.println(blockingQueue.take());
            System.out.println(blockingQueue.take());
            System.out.println(blockingQueue.take());
            // 没有这个元素，一直等待
            System.out.println(blockingQueue.take());
            System.out.println("============");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    /**
     * 超时等待
     */
    public static void test04() {
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);


        try {
            System.out.println(blockingQueue.offer("a"));
            System.out.println(blockingQueue.offer("b"));
            System.out.println(blockingQueue.offer("c"));
            //等待两秒就退出
            System.out.println(blockingQueue.offer("d", 2, TimeUnit.SECONDS));

            System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS));
            System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS));
            System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS));
            System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
